package cn.lil.servlet;

import cn.lil.model.Bearing;
import cn.lil.model.Order;
import cn.lil.service.BearingService;
import com.oracle.wls.shaded.org.apache.xpath.operations.Or;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.SQLException;
@WebServlet(name = "BearingBuyServlet",value = "/BearingBuyServlet")
public class BearingBuyServlet extends HttpServlet {
    BearingService bearingService = new BearingService();
    Bearing bearing = null;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order o = null;
        if(request.getSession().getAttribute("order") != null){
            o = (Order) request.getSession().getAttribute("order");
        }else{
            o = new Order();
            request.getSession().setAttribute("order",o);
        }
        int bearingId = Integer.parseInt(request.getParameter("bearingId"));
        try {
            bearing = bearingService.selectBearingById(bearingId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(bearing.getInventory()>0){
            o.addBearing(bearing);
            response.getWriter().write("ok");
        }else{
            response.getWriter().write("fail");

        }
    }
}
